package MyCustomTable;

import MyUtil.*;

import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.util.List;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Best_Selling_Book_List extends JFrame{
    private String[] headName = {"图书姓名","图书作者","图书分类","出版社","图书数量"};
    private JLabel jLabel = new JLabel("到");
    private java.util.Date d1;
    private java.util.Date d2;

    private TreeSet<java.util.Date> day_Set = new TreeSet();
    private ResultSet rs;

    private JComboBox<java.util.Date> dayJComboBox1 = new JComboBox<>();
    private JComboBox<java.util.Date> dayJComboBox2 = new JComboBox<>();

    private JPanel jPanel;
    private JButton query_Button;
    private JButton output_Button;
    private JScrollPane jScrollPane = new JScrollPane();

    public Best_Selling_Book_List() throws SQLException {
        //本体设置布局
        this.setBounds(400,150,1000,700);
        //控件
        jPanel = new MyPanel("/picture/3.jpg");
        setContentPane(jPanel);
        //设置布局
        jPanel.setLayout(new MyDearLayout());
        //日期
        rs = DataBaseUtil.produceResultSet("select Pdate from Pay_List");
        while(rs.next()){
            java.util.Date d1 = rs.getDate("Pdate");
            //日期添加
            day_Set.add(d1);
        }
        //按钮
        query_Button = new JButton("查询");
        output_Button = new JButton("导出");
        output_Button.setEnabled(false);
        //JCombo的建立
        for (Date d1:day_Set){
            dayJComboBox1.addItem(d1);
            dayJComboBox2.addItem(d1);
        }
        // 标签已建立
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //添加控件
        jPanel.add(query_Button);
        jPanel.add(dayJComboBox1);
        jPanel.add(jLabel);
        jPanel.add(dayJComboBox2);
        jPanel.add(output_Button);
        query_Button.addActionListener((e)->{
            query_jbutton_FUNCTION();
        });
        output_Button.addActionListener((e)->{
            try {
                output_jbuttion_FUNCTION();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        });
    }
    public void query_jbutton_FUNCTION(){
        d1 = (java.util.Date)dayJComboBox1.getSelectedItem();
        d2 = (java.util.Date)dayJComboBox2.getSelectedItem();
        String sql = "select Book.Bname 图书姓名 ,Book.Bauthor 图书作者,Book.Btype 图书分类,Book.Bpress  出版社,sum(Pay_List.Pnumber) 图书数量\n"+"from Book,Pay_List\n"+ "where Pay_List.Bid = Book.Bid and to_char(Pdate,'yyyy-mm-dd') between '"+d1 +"'and '"+d2+"'\n"+ "group by Book.Bid,Book.Bname,Book.Bauthor,Book.Btype,Book.Bpress\n"+ "order by sum(Pnumber) desc";
        jPanel.remove(jScrollPane);
        try {
            rs = DataBaseUtil.produceResultSet(sql);
            jScrollPane =JScrollUtil.getCustomJScrollPane(rs);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        jPanel.add(jScrollPane);
        output_Button.setEnabled(true);
        this.setVisible(true);
    }
    public void output_jbuttion_FUNCTION() throws SQLException {
        CSVUtil.searchSavePath(this,ObjectListUtil.getBestSellingDataList(rs),headName,d1+"到"+d2+"畅销书表");
    }
    private class MyDearLayout implements LayoutManager{

        @Override
        public void addLayoutComponent(String name, Component comp) {

        }

        @Override
        public void removeLayoutComponent(Component comp) {

        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return null;
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return null;
        }

        @Override
        public void layoutContainer(Container parent) {
            int width = getWidth();
            int height = getHeight();
            if(dayJComboBox1.isVisible()){
                dayJComboBox1.setBounds(width/4,height/50,120,50);
            }
            if(jLabel.isVisible()){
                jLabel.setBounds(width/4+140,height/50,40,50);
            }
            if(dayJComboBox2.isVisible()){
                dayJComboBox2.setBounds(width/4+200,height/50,120,50);
            }
            if(query_Button.isVisible()){
                query_Button.setBounds(width/4+350,height/50,60,60);
            }
            if(output_Button.isVisible()){
                output_Button.setBounds(width/4+420,height/50,60,60);
            }
            if(jScrollPane.isVisible()){
                jScrollPane.setBounds(50,height/4,width-100,height/5*3);
            }

        }
    }

}
